﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Expression.Encoder.Profiles;
using Microsoft.Expression.Encoder;
using System.Windows.Threading;

namespace VideoEncoder
{
    /// <summary>
    /// Interaction logic for AudioProfileControl.xaml
    /// </summary>
    public partial class AudioProfileControl : UserControl
    {
        private MediaItem mediaItem;
        public MediaItem MediaItem
        {
            get { return mediaItem; }
            set
            {
                if (mediaItem != null)
                    mediaItem.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(mediaItem_PropertyChanged);
                mediaItem = value;
                if (mediaItem == null)
                    return;
                DataContext = mediaItem;
                audioProfileCombo.SelectedValue = value.AudioProfile;
                mediaItem.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mediaItem_PropertyChanged);
                SetIsEnabled();
            }
        }

        public ObservableCollection<AudioProfile> AudioProfiles { get; set; }
        public AudioProfileControl()
        {
            InitializeComponent();
            audioProfileCombo.SelectionChanged += new SelectionChangedEventHandler(audioProfileCombo_SelectionChanged);
            Loaded += new RoutedEventHandler(AudioProfileControl_Loaded);
        }

        void audioProfileCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (audioProfileCombo.SelectedIndex == -1)
                return;
            MediaItem.AudioProfile = AudioProfiles[audioProfileCombo.SelectedIndex];
        }

        void AudioProfileControl_Loaded(object sender, RoutedEventArgs e)
        {
            audioProfileCombo.ItemsSource = AudioProfiles;
        }

        void mediaItem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Status")
                SetIsEnabled();
        }
        private void SetIsEnabled()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                if (MediaItem.Status == ItemStatus.Encoding)
                {
                    audioProfileCombo.IsEnabled = false;
                }
                else
                {
                    audioProfileCombo.IsEnabled = true;
                }
            });
        }
    }
}
